Soru & Cevap

Buton Ekleme,sayfa değiştirme

11.02.2016 - 10:46

public class HomeFragment extends Fragment {
	
	public HomeFragment(){}
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
 
        View rootView = inflater.inflate(R.layout.fragment_home, container, false);
         
        return rootView;
    }
}

 

Buna buton eklemek istiyorum başka bir sayfaya geçiş için ama naptıysam olmuyor.

 

public class HomeFragment extends Fragment {
	
	Button button1;
	public HomeFragment(){}
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
 
        View rootView = inflater.inflate(R.layout.fragment_home, container, false);
         
        final Context context = this;
        button1 = (Button) findViewById(R.id.imageButton1);
        button1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(context, Bilimsel.class);
                startActivity(intent);
            }
        });
        return rootView;
    }

	private Button findViewById(int imagebutton1) {
		// TODO Auto-generated method stub
		return null;
	}
}

 

 final Context context = this;

kısmı hata veriyor.

 

 

241 Görüntülenme

8 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.

Profile picture for user alikoparan
alikoparan
12.02.2016 - 10:40

Bi el atacak yok mu yaw kaldım öyle bi buton yüzünden

Profile picture for user alikoparan
alikoparan
11.02.2016 - 03:40
02-11 15:37:21.130: D/dalvikvm(12121): Late-enabling CheckJNI
02-11 15:37:22.755: D/dalvikvm(12121): GC_FOR_ALLOC freed 207K, 13% free 9742K/11136K, paused 40ms, total 40ms
02-11 15:37:22.755: I/dalvikvm-heap(12121): Grow heap (frag case) to 11.742MB for 1127536-byte allocation
02-11 15:37:22.880: D/dalvikvm(12121): GC_FOR_ALLOC freed <1K, 12% free 10843K/12240K, paused 124ms, total 124ms
02-11 15:37:23.035: D/AbsListView(12121): Get MotionRecognitionManager
02-11 15:37:23.110: D/dalvikvm(12121): GC_FOR_ALLOC freed 21K, 12% free 10879K/12240K, paused 16ms, total 16ms
02-11 15:37:23.110: I/dalvikvm-heap(12121): Grow heap (frag case) to 13.149MB for 1440016-byte allocation
02-11 15:37:23.140: D/dalvikvm(12121): GC_FOR_ALLOC freed 0K, 10% free 12285K/13648K, paused 22ms, total 22ms
02-11 15:37:23.240: D/dalvikvm(12121): GC_FOR_ALLOC freed 1408K, 15% free 16504K/19276K, paused 14ms, total 14ms
02-11 15:37:23.260: D/dalvikvm(12121): GC_FOR_ALLOC freed <1K, 13% free 16894K/19276K, paused 14ms, total 14ms
02-11 15:37:23.260: I/dalvikvm-heap(12121): Grow heap (frag case) to 19.176MB for 1600016-byte allocation
02-11 15:37:23.300: D/dalvikvm(12121): GC_FOR_ALLOC freed 0K, 12% free 18457K/20840K, paused 36ms, total 36ms
02-11 15:37:23.355: D/AndroidRuntime(12121): Shutting down VM
02-11 15:37:23.355: W/dalvikvm(12121): threadid=1: thread exiting with uncaught exception (group=0x4196b700)
02-11 15:37:23.365: E/AndroidRuntime(12121): FATAL EXCEPTION: main
02-11 15:37:23.365: E/AndroidRuntime(12121): java.lang.RuntimeException: Unable to start activity ComponentInfo{hemsirelik.gunleri/hemsirelik.gunleri.MainActivity}: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.Button
02-11 15:37:23.365: E/AndroidRuntime(12121): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
02-11 15:37:23.365: E/AndroidRuntime(12121): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
02-11 15:37:23.365: E/AndroidRuntime(12121): 	at android.app.ActivityThread.access$700(ActivityThread.java:159)
02-11 15:37:23.365: E/AndroidRuntime(12121): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
02-11 15:37:23.365: E/AndroidRuntime(12121): 	at android.os.Handler.dispatchMessage(Handler.java:99)
02-11 15:37:23.365: E/AndroidRuntime(12121): 	at android.os.Looper.loop(Looper.java:176)
02-11 15:37:23.365: E/AndroidRuntime(12121): 	at android.app.ActivityThread.main(ActivityThread.java:5419)
02-11 15:37:23.365: E/AndroidRuntime(12121): 	at java.lang.reflect.Method.invokeNative(Native Method)
02-11 15:37:23.365: E/AndroidRuntime(12121): 	at java.lang.reflect.Method.invoke(Method.java:525)
02-11 15:37:23.365: E/AndroidRuntime(12121): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
02-11 15:37:23.365: E/AndroidRuntime(12121): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
02-11 15:37:23.365: E/AndroidRuntime(12121): 	at dalvik.system.NativeStart.main(Native Method)
02-11 15:37:23.365: E/AndroidRuntime(12121): Caused by: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.Button
02-11 15:37:23.365: E/AndroidRuntime(12121): 	at hemsirelik.gunleri.HomeFragment.onCreateView(HomeFragment.java:22)
02-11 15:37:23.365: E/AndroidRuntime(12121): 	at android.app.Fragment.performCreateView(Fragment.java:1699)
02-11 15:37:23.365: E/AndroidRuntime(12121): 	at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
02-11 15:37:23.365: E/AndroidRuntime(12121): 	at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
02-11 15:37:23.365: E/AndroidRuntime(12121): 	at android.app.BackStackRecord.run(BackStackRecord.java:682)
02-11 15:37:23.365: E/AndroidRuntime(12121): 	at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1455)
02-11 15:37:23.365: E/AndroidRuntime(12121): 	at android.app.Activity.performStart(Activity.java:5381)
02-11 15:37:23.365: E/AndroidRuntime(12121): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)
02-11 15:37:23.365: E/AndroidRuntime(12121): 	... 11 more

 

Profile picture for user alikoparan
alikoparan
11.02.2016 - 02:41

Evet ekledim activity

Tunahan Tolga Yıldız
11.02.2016 - 03:03
Hataları paylaşabilir misin.
Ali Koparan
11.02.2016 - 03:40
Uygulama açılıyor mainactivity geliyor snr homefragment geçicekken kapanıyor.
picture-62423-1460561213.jpg
emre.tsbs
11.02.2016 - 02:32

AndroidManifest.xml de gerekli düzenlemeleri yaptın mı?

Profile picture for user alikoparan
alikoparan
11.02.2016 - 11:47

Aynen kopyaladım yine hata veriyor.

Uygulama aılıyor durduruluyor.

Profile picture for user tunahantolga
tunahantolga
11.02.2016 - 11:44
public class HomeFragment extends Fragment {
	
	Button button1;
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
 
        View rootView = inflater.inflate(R.layout.fragment_home, container, false);
         
        button1 = (Button) rootView.findViewById(R.id.imageButton1);
        button1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(getActivity(), Bilimsel.class);
                startActivity(intent);
            }
        });
        return rootView;
    }
}

Bu şekilde çalışması lazım.

Profile picture for user alikoparan
alikoparan
11.02.2016 - 11:34

Denedim olmuyor 

return rootView; den kaynaklı sorun gibi ama çok bilgim yok çözemiyorum.

Team pc girip ya da dosyayı göndersem yardımcı olabilcek var mı?

Tunahan Tolga Yıldız
11.02.2016 - 11:45
Fragment'i extend ettiğinde import kısmı böyle değil mi? import android.support.v4.app.Fragment;
Ali Koparan
11.02.2016 - 11:55
android.support.v4.app.Fragment; yaptığımda main activity de hata veriyor * Diplaying fragment view for selected nav drawer list item * */ private void displayView(int position) { // update the main content by replacing fragments Fragment fragment = null; switch (position) { case 0: fragment = new HomeFragment(); break;
Profile picture for user tunahantolga
tunahantolga
11.02.2016 - 11:06

final Context context = this; bu satırı kullanmadan, bu şekilde yapabilirsin.

button1.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v){
        Intent intent = new Intent(getActivity(),Bilimsel.class);
        getActivity().startActivity(intent);
    }
});